//
//  ContainerTabController.m
//  CardMoveGame
//
//  Created by peac on 2025/11/5.
//

#import "ContainerTabController.h"
#import "CPSHDrawViewController.h"
#import "CPSHHeroSelect.h"
#import "CPSHShop.h"
#import "CPSHTasksViewController.h"

#import "GameViewController.h"
@interface ContainerTabController ()

@property (nonatomic, strong) CustomTabBar *customTabBar;
@property (nonatomic, strong) UIViewController *currentViewController;
@property (nonatomic, strong) NSArray *viewControllers;
@property (nonatomic, strong) UINavigationController *centerNavigationController;
@end

@implementation ContainerTabController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupViewControllers];
    [self setupCustomTabBar];
    [self selectTabAtIndex:2];
}

- (void)setupViewControllers {
    NSMutableArray *vcs = [NSMutableArray array];
    
    CPSHDrawViewController *mddrawvc=[[CPSHDrawViewController alloc]init];
    [vcs addObject:mddrawvc];
    CPSHHeroSelect *mdherovc=[[CPSHHeroSelect alloc]init];
    [vcs addObject:mdherovc];
    
    ViewController *mdcityvc=[[ViewController alloc]init];
    mdcityvc.pushdelegate = self;

    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mdcityvc];
    nav.navigationBarHidden = YES;
    self.centerNavigationController = nav;
    
    [vcs addObject:nav];
    CPSHTasksViewController *mdtaskvc=[[CPSHTasksViewController alloc]init];
    [vcs addObject:mdtaskvc];
    CPSHShop *mdshopvc=[[CPSHShop alloc]init];
    [vcs addObject:mdshopvc];
    
    
    self.viewControllers = vcs;
}

- (void)setupCustomTabBar {
    
  
    
    CGFloat tabBarHeight = self.view.frame.size.height*0.12;
 
    
    self.customTabBar = [[CustomTabBar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - tabBarHeight, self.view.frame.size.width, tabBarHeight)];
    self.customTabBar.delegate = self;
    
    NSArray *titles = @[@"首页", @"发现", @"消息", @"我的", @"设置"];
    NSArray *images = @[@"draw", @"hero", @"city", @"task", @"shop"];
    NSArray *selectedImages = @[@"tab_home_selected", @"tab_discover_selected", @"tab_message_selected", @"tab_profile_selected", @"tab_settings_selected"];
    
    [self.customTabBar setupTabBarItemsWithTitles:titles images:images selectedImages:selectedImages];
    [self.view addSubview:self.customTabBar];
}

- (void)selectTabAtIndex:(NSInteger)index {
    if (self.currentViewController) {
        [self.currentViewController willMoveToParentViewController:nil];
        [self.currentViewController.view removeFromSuperview];
        [self.currentViewController removeFromParentViewController];
    }
    
    UIViewController *selectedVC = self.viewControllers[index];
    [self addChildViewController:selectedVC];
    selectedVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.customTabBar.frame.size.height*0.67);
    [self.view insertSubview:selectedVC.view belowSubview:self.customTabBar];
    [selectedVC didMoveToParentViewController:self];
    
    self.currentViewController = selectedVC;
    NSInteger aa = self.centerNavigationController.viewControllers.count;
    if (index == 2 && self.centerNavigationController.viewControllers.count > 1) {
         [self.centerNavigationController popToRootViewControllerAnimated:NO];
     }
    
    
}

#pragma mark - CustomTabBarDelegate

- (void)tabBarDidSelectItemAtIndex:(NSInteger)index {
    [self selectTabAtIndex:index];
}


- (void)centerViewControllerDidmagicshotPush{
    GameViewController *mdlgVc=[[GameViewController alloc]init];
    mdlgVc.view.backgroundColor = [UIColor clearColor];
    [self.centerNavigationController pushViewController:mdlgVc animated:YES];

}
- (void)centerViewControllerDiddispelmagicPush{

    
    
}
- (void)centerViewControllerDidchallenegPush{

}

@end
